<template>
  <div class="user-edit-container">
    <el-card>
      <div slot="header">
        <span>{{ isEdit ? '编辑用户' : '添加用户' }}</span>
      </div>

      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>

        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password" v-if="!isEdit">
          <el-input v-model="form.password" type="password"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>

        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>

        <el-form-item label="角色" prop="role">
          <el-select v-model="form.role" placeholder="请选择角色">
            <el-option label="管理员" :value="1"></el-option>
            <el-option label="教师" :value="2"></el-option>
            <el-option label="学生" :value="3"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm">保存</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { addUser, updateUser, getUserDetail } from '@/api/user'

export default {
  name: 'UserEdit',
  data() {
    return {
      isEdit: false,
      form: {
        username: '',
        realName: '',
        password: '',
        email: '',
        phone: '',
        role: 3,
        status: 1
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        realName: [
          { required: true, message: '请输入真实姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        email: [
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    if (this.$route.params.id) {
      this.isEdit = true
      this.getUserDetail(this.$route.params.id)
    }
  },
  methods: {
    getUserDetail(id) {
      getUserDetail(id).then(response => {
        this.form = response.data
      })
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            updateUser(this.$route.params.id, this.form).then(() => {
              this.$message.success('更新成功')
              this.$router.push('/user/list')
            })
          } else {
            addUser(this.form).then(() => {
              this.$message.success('添加成功')
              this.$router.push('/user/list')
            })
          }
        }
      })
    },
    cancel() {
      this.$router.push('/user/list')
    }
  }
}
</script>

<style lang="scss" scoped>
.user-edit-container {
  padding: 20px;

  .el-form {
    width: 600px;
  }
}
</style>